<?php
class Purchase extends AppModel {

	var $name = 'Purchase';
	var $useTable = 'purchases';

	var $order = array('Purchase.date DESC, Purchase.created DESC');

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
		'Category' => array('className' => 'Category',
							'foreignKey' => 'category_id',
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'counterCache' => true,
							'sumCache' => true
		),
		'PaymentType' => array('className' => 'PaymentType',
							'foreignKey' => 'payment_type_id',
							'conditions' => '',
							'fields' => '',
							'order' => ''
		),
		'User' => array('className' => 'User',
							'foreignKey' => 'user_id',
							'conditions' => '',
							'fields' => '',
							'order' => ''
		)
	);

	var $validate = array(
		'description' => array(
			'rule' => 'notEmpty',
			'message' => 'You need to enter a description'
		),
		'amount' => array(
			'rule' => array('decimal', 2),
			'message' => 'You need to enter the amount'
		),
		'date' => array(
			'rule' => 'date',
			'allowEmpty' => false,
			'message' => 'You need to enter the date'
		),
		'category_id' => array(
			'rule' => 'numeric',
			'allowEmpty' => false,
			'message' => 'You need to choose a category'
		)
	);

/*	function beforeDelete() {
		$this->log('beforeDelete'.pr($this->data, true));
		return true;
	}
*/
	function updateCounterCache($keys = array(), $created = false) {

		parent::updateCounterCache($keys);

		if (isset($this->data['Purchase'])) {

			$this->log('Purchase::updateCounterCache');
			$this->log(array_keys($this->data));

			$cat_id = $this->data['Purchase']['category_id'];
			$date = $this->data['Purchase']['date'];

			// UPDATE MONTH TOTALS
			$dts = strtotime($date);
			$month = date('Y-m-01',$dts);
			$dts = strtotime($month);
			$next_month = date('Y-m-01', strtotime('+1 month', $dts));

			$month_sum = $this->find('first',
				array('fields'=>'SUM(amount) as amount',
					'conditions'=>array("Purchase.date >= '$month'", "Purchase.date < '$next_month'", 'Purchase.category_id'=>$cat_id)
				)
			);
			$month_sum = Set::extract($month_sum, 'Purchase.amount');

			App::Import('Model', 'MonthTotal');
			$m = new MonthTotal();
			$data = array('MonthTotal' => array('id' => $month.'-'.$cat_id, 'date'=>$month, 'category_id'=>$cat_id, 'amount' => $month_sum));
			$m->save($data);


			// UPDATE YEAR TOTALS
			$year = date('Y',strtotime($date)).'-01-01';

			$dts = strtotime($date);
			$year = date('Y-01-01',$dts);
			$dts = strtotime($year);
			$next_year = date('Y-01-01', strtotime('+1 year', $dts));

			$year_sum = $this->find('first',
				array('fields'=>'SUM(amount) as amount',
					'conditions'=>array("Purchase.date >= '$year'", "Purchase.date < '$next_year'", 'Purchase.category_id'=>$cat_id)
				)
			);
			$year_sum = Set::extract($year_sum, 'Purchase.amount');

			App::Import('Model', 'YearTotal');
			$m = new YearTotal();
			$data = array('YearTotal' => array('id' => $year.'-'.$cat_id, 'date'=>$year, 'category_id'=>$cat_id, 'amount' => $year_sum));
			$m->save($data);

		}

	}

}
?>